<?php
/**
 * @author yuanyitian
 * @date 2024/8/13 20:04
 */

namespace api\admin;

class category
{
    //测试
    public function getList1($data){
        var_dump($data);
        return out_right($data);
    }

    //分页列表
    public function getList($requst){
        $con=con();

        $name=$requst['name'];
        $page=(int)$requst['page'];
        $size=(int)$requst['size'];
        $start=($page-1)*$size;

        $where=' where is_delete=1 ';
        if(!empty($name)){
            $where.=" and name like '%$name%'";
        }
        $sql="select * from category $where";
        $data=mysqli_query($con,$sql." order by `id` DESC limit $start,$size");
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);
        mysqli_close($con);
        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size)
        ]);
    }

    //添加分类
    public function add($request){
        $name=$request['name'];
        $sort=(int)$request['sort'];
        $remark=$request['remark'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"insert into category(name,sort,remark,create_time) values('$name',$sort,'$remark',$time)");
        mysqli_close($con);

        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //编辑分类
    public function edit($request){
        $id=(int)$request['id'];
        $name=$request['name'];
        $sort=(int)$request['sort'];
        $remark=$request['remark'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"update category set name='$name',sort=$sort,remark='$remark',update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    //删除分类
    public function del($request){
        $id=(int)$request['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update category set is_delete=0,delete_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    //修改分类状态
    public function changeStatus($request){
        $status=(int)$request['status'];
        $id=(int)$request['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update category set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    //全量列表
    public function getAll($data){
        $con=con();
        $data=mysqli_query($con,"select * from category where is_delete=1 and status=1 order by sort DESC");
        $data=mysqli_fetch_all($data,true);
        mysqli_close($con);
        return out_right($data);
    }



}